package com.cqc.decimaldemo;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
	private EditText editText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editText = (EditText) findViewById(R.id.editText);
		
		editText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) { }
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
			
			@Override
			public void afterTextChanged(Editable edt) {
                String temp = edt.toString();  
                int posDot = temp.indexOf(".");  
                if (posDot <= 0) return;  
                if (temp.length() - posDot - 1 > 3) { //如果小数点后超过3位
                    edt.delete(posDot + 3, posDot + 4);//用第posDot + 4替换第posDot + 3
                }  
			}
		});	
		 
		 Button btn = (Button) findViewById(R.id.btn);
		 btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("tag", "edittext="+editText.getText().toString().trim());
			}
		});
	}
}
